
$(function(){
	music_search_index.run();
});

function showPlayer(obj){
	$(".mp3Download_playBox").html("").removeClass("mp3Download_playBox");
	var file=$(obj).attr("file");
	var filename=$(obj).attr("filename");
	var html = '<div class="music-result-play"><embed id="ply" width="295" height="20" flashvars="'+file+'" allowscriptaccess="always" quality="high" name="ply" style="" src="http://www.4shared.com/flash/player.swf?ver=9051" type="application/x-shockwave-flash"></div>';
	html = html + '<dl><dt>Embed this song to your web</dt><dd><input value=\"'+filename+'<br/><embed id=\'ply\' width=\'295\' height=\'20\' flashvars=\''+file+'\' wmode=\'opaque\' allowfullscreen=\'false\' allowscriptaccess=\'always\' quality=\'high\' name=\'ply\' src=\'http://www.4shared.com/flash/player.swf?ver=9051\' type=\'application/x-shockwave-flash\'><br/>Powered by <a href=\'http://www.wontube.com/\'>wontube.com</a>" class="input_txt" type="text"  onfocus="this.select()" /><input class="btn copy-button" name="Submit" type="submit" value="Copy" /></dd></dl>';
	html = html + '<span class="btn_close"></span>';
	$(obj).next().next().next().append(html).addClass("mp3Download_playBox");
	
}

function copyToClipboard(obj) {
	var txt=$(obj).prev().val();
    if(window.clipboardData) {   
            window.clipboardData.clearData();   
            window.clipboardData.setData("Text", txt);   
    } else if(navigator.userAgent.indexOf("Opera") != -1) {   
         window.location = txt;   
    } else if (window.netscape) {   
         try {   
              netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");   
         } catch (e) {   
              alert("Browser Refused！\n Please in the browser address bar type in 'about:config' than 'enter'\n seach 'signed.applets.codebase_principal_support' and set 'true'");   
         }   
         var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);   
         if (!clip)   
              return;   
         var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   
         if (!trans)   
              return;   
         trans.addDataFlavor('text/unicode');   
         var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);   
         var copytext = txt;   
         str.data = copytext;   
         trans.setTransferData("text/unicode",str,copytext.length*2);   
         var clipid = Components.interfaces.nsIClipboard;   
         if (!clip)   
              return false;   
         clip.setData(trans,null,clipid.kGlobalClipboard);   
         alert("copy success !");   
    }   
}  




var music_search_index = {
		run: function(){
			this._init_();
			this._start_();
			this._init_event_();
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val();
			this.query = $("#query").val();
			
		},
		
		_start_: function(){
			music_top.run();
			if(this.query){
				$("#mprDownload_search > input.smenu-input").val(this.query).blur();
				var reg = new RegExp("(" + this.query + ")", 'gi');
				$("#mp3Download_cont_mg > div.mp3Download_list > ul > li").find(".music_title").each(function(){
					var $this = $(this);
					var old = $this.html();
					var n = old.replace(reg, "<span class=\"t_red\">$1</span>");
					$this.html(n);
				});
			}
		},
		
		_init_event_: function(){
			$("#mprDownload_search").smenu({
				backend : 'fshared',
				defaultValue : 'Search for Music...',
				onchange : function(query) {
					var sreg = /[`~!@#\$%\^&\*\(\)\+=\[\{\]\};:\<\>\?\/\\\|"]+/g;
					query = query.replace(sreg, "");
					location.href = main.contextPath + "/mg/mp3/" + query;
				}
			});
			$("#page_box").find("a").click(function(){
				var $this = $(this);
				music_search_index._query_music(music_search_index.query,$this.attr("page"));
				return false;
			});
		},
		
		_query_music: function(query, page){
			var sreg = /[`~!@#\$%\^&\*\(\)\+=\[\{\]\};:\<\>\?\/\\\|"]+/g;
			query = query.replace(sreg, "");
			if (query) {
				query = encodeURIComponent(query);
				page = page || 1;
				var url = this.contextPath + "/mg/mp3/search?page=" + page  + "&query=" + query;
				$.get(url, function(result){
					$("#mp3Download_cont_mg").html(result);
					music_top.run();
					var queryStr = decodeURIComponent(query);
			
					$("#mprDownload_search > input.smenu-input").val(queryStr).blur();
					var reg = new RegExp("(" + queryStr + ")", 'gi');
					$(".music_title").each(function(){
						var $this = $(this);
						var old = $this.html();
						var n = old.replace(reg, "<span class=\"t_red\">$1</span>");
						$this.html(n);
					});
					$("#page_box").find("a").click(function(){
						var $this = $(this);
						music_search_index._query_music(queryStr,$this.attr("page"));
						return false;
					});
				});
			}
		}
};

var music_top = {
		run: function(){
			$('.btn_play3').click(function(){
				showPlayer(this);
				var title=$("#query").val();
				main.addOrUpdateViewHistory("mp3",title,1);
				_gaq.push(['_trackEvent', 'music', 'Play', title]);
			});
			
			$('.copy-button').live('click',function(){
				copyToClipboard(this);
			});
			
			$(".btn_close").live('click',function(){
				$(".mp3Download_playBox").html("").removeClass("mp3Download_playBox");
			});
		}
};
